package Java基础2;

import org.w3c.dom.NodeList;

public class MyNodeList<E> {
    private class ListNode<E> {
    Object val;
    ListNode next = null;
    ListNode(Object x) { val = x; }
    }

    /**
     * 定义头结点和指针
     */
    private ListNode firstNode = null;//头结点
    private ListNode nowNode = null;//尾节点
    private ListNode pointNode = null;//指针节点

    /**
     * 添加方法 用来添加一个node到节点里头
     * @param node
     */
    public void addNode(E node){
        if (firstNode==null) {
            firstNode = new ListNode(node);//创建头结点
            nowNode = firstNode;//浮动游标
        }
        else {
            ListNode a= new ListNode(node);//创建节点
            nowNode.next = a;//修改目标
            nowNode = nowNode.next;//改变指针位置
        }

    }

    /**
     * 定位添加
     * @param x
     * @param node
     * @return
     */
    public E addNode(int x,E node){
        if (x<0||x>this.getListSize()) return null;
        pointNode = firstNode;
        ListNode l = new ListNode(node);
        if(x==1){
            l.next = firstNode;
            firstNode = l;
            return node;
        }
        for(int i=1;i<x;i++){
            if(i==x-1){
                l.next = pointNode.next;
                pointNode.next = l;
                return node;
            }
            pointNode = pointNode.next;
        }
        return null;
    }
    /**
     * 在指定的节点之后添加
     * @param Old 原
     * @param NewL 目标节点值
     */
    public E addNodeTo(E Old,E NewL){
        ListNode OldNode = new ListNode(Old);
        ListNode NewNode = new ListNode(NewL);
        pointNode = firstNode;
        while (true){
            if(pointNode.next.val.equals(OldNode.val)){
                NewNode.next = pointNode.next;
                pointNode.next = NewNode;
                return NewL;
            }
            if (pointNode.next==null) break;
            pointNode = pointNode.next;
        }
        return null;
    }

    /**
     * 修改节点
     */
    public boolean replacce(E Old,E NewL){
        ListNode OldNode = new ListNode(Old);
        ListNode NewNode = new ListNode(NewL);
        pointNode = firstNode;
        if(OldNode.val.equals(firstNode.val))//拦截特殊值
            {
                NewNode.next = firstNode.next;
                firstNode = NewNode;
                return true;
            }
        while (true){
            if(pointNode.next.val.equals(OldNode.val)){
                NewNode.next = pointNode.next.next;
                pointNode.next = NewNode;
                return true;
            }
            if (pointNode.next==null) break;
            pointNode = pointNode.next;
        }
        return false;
    }

    /**
     * 输出方法
     * @return
     */
    @Override
    public String toString() {
        String s = "";
        pointNode = firstNode;
        if (pointNode==null) return null;
        while (true){
            s=s+pointNode.val.toString();
            if (pointNode.next==null) break;
            s=s+"→";
            pointNode = pointNode.next;
        }
            return s;
    }


    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }

    /**
     * 删除节点
     * @param node1
     */
    public E deleteNode(E node1) {
        if (pointNode==null) return null;
        ListNode node = new ListNode(node1);
        pointNode = firstNode;
        if (node.equals(nowNode))
        {
            while (!pointNode.next.equals(nowNode)){
                nowNode = pointNode;
            }
        }
        while (true){
            if(pointNode.next.val.equals(node.val)){
                pointNode.next = pointNode.next.next;
                return node1;
            }
            if (pointNode.next==null) break;
            pointNode = pointNode.next;
        }
        return null;
    }

    /**
     * 定序删除
     * @param num
     */
    public void deleteNode(int num) {
        pointNode = firstNode;
        if (pointNode==null) return;
        for(int i=1;i<num;i++){
            if(i==num-1){
                pointNode.next = pointNode.next.next;
                return;
            }
            pointNode = pointNode.next;
        }

    }

    /**
     * 获取链表长度
     * @return
     */
    public int getListSize(){
        int a = 0;
        if (firstNode==null) return a;
        pointNode = firstNode;
        a=1;
        while (pointNode.next!=null){
            a++;
            pointNode = pointNode.next;
        }
        return a;
    }

    /**
     * 清空列表
     */
    public void empty(){
        firstNode = null;
        nowNode = firstNode;
    }
}
